package com.easy.controller;

import com.easy.bean.Member;
import com.easy.bean.Register;
import com.easy.bean.Trainer;
import com.easy.common.CommonResult;
import com.easy.service.IMemberService;
import com.easy.service.IRegisterService;
import com.easy.service.ITrainerService;
import com.easy.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class RegisterController {

    @Autowired
    IRegisterService registerS;
    @Autowired
    IMemberService memberS;
    @Autowired
    ITrainerService trainerS;


    @GetMapping("register")
    public CommonResult getRegister(Register register, Page page){
        List<Register> list = registerS.getRegister(register,page);
        int count = registerS.getRegisterCount(register);
        HashMap map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return CommonResult.success(map);
    }

    @DeleteMapping("register")
    public CommonResult delRegisterByID(@RequestBody Register register){
        int res = registerS.delRegisterByID(register);
        return CommonResult.success(res);
    }

    @PostMapping("register")
    public CommonResult addRegister(@RequestBody Register register){
        System.out.println(register.getMemberPhoneNumber());
        System.out.println(register.getTrainerPhoneNumber());
        Member member = new Member();
        Trainer trainer = new Trainer();
        Page page = new Page();
        member.setPhoneNumber(register.getMemberPhoneNumber());
        trainer.setPhoneNumber(register.getTrainerPhoneNumber());
        List<Member> listMember = memberS.getMember(member,page);
        List<Trainer> listTrainer = trainerS.getTrainer(trainer,page);
        if(listMember.size() == 1&&listTrainer.size() == 1){
            register.setMemberID(listMember.get(0).getMemberID());
            register.setTrainerID(listTrainer.get(0).getTrainerID());
        }else {
            return CommonResult.fail();
        }
        register = registerS.getRegisterByID(register);
        if(register == null){
            registerS.addRegister(register);
            register = registerS.getRegisterByID(register);
            return CommonResult.success(register);
        }
        return CommonResult.fail(666);
    }

    @PutMapping("register")
    public CommonResult editRegister(@RequestBody Register register){
        Member member = new Member();
        Trainer trainer = new Trainer();
        Page page = new Page();
        member.setPhoneNumber(register.getMemberPhoneNumber());
        trainer.setPhoneNumber(register.getTrainerPhoneNumber());
        List<Member> listMember = memberS.getMember(member,page);
        List<Trainer> listTrainer = trainerS.getTrainer(trainer,page);
        if(listMember.size() == 1&&listTrainer.size() == 1){
            register.setMemberID(listMember.get(0).getMemberID());
            register.setTrainerID(listTrainer.get(0).getTrainerID());
        }else {
            return CommonResult.fail();
        }
        registerS.editRegister(register);
        register = registerS.getRegisterByID(register);
        return CommonResult.success(register);
    }
}
